package com.eternal.base.data.source;

import androidx.paging.DataSource;

import com.eternal.base.concat.DeviceInfo;
import com.eternal.base.concat.TmpHum;
import com.eternal.base.database.BaseDatabase;
import com.eternal.base.database.dao.HistoryDao;
import com.eternal.base.database.entity.History;

import java.util.List;

import io.reactivex.Flowable;

public class HistorySource implements IHistorySource {
    private HistoryDao dao;

    public HistorySource() {
        dao = BaseDatabase.getInstance().historyDao();
    }

    @Override
    public DataSource.Factory<Integer, TmpHum> getHistory(String mac) {
        return dao.loadAllHistoryFromMac(mac);
    }

    @Override
    public DataSource.Factory<Integer, TmpHum> getHistoryMin(String mac, List<String> times, long start, long end) {
        return dao.loadAllHistoryFromMacMin(mac, times, start, end);
    }

    @Override
    public DataSource.Factory<Integer, TmpHum> getHistoryHour(String mac, List<String> times, String min, long start, long end) {
        return dao.loadAllHistoryFromMacHour(mac, times, min, start, end);
    }

    @Override
    public DataSource.Factory<Integer, TmpHum> getHistory(String mac, long start, long end) {
        return dao.loadAllHistoryFromMac(mac, start, end);
    }

    @Override
    public void add(History history) {
        dao.insert(history);
    }

    @Override
    public long getTime(String mac) {
        return dao.loadLastTime(mac) * 1000L;
    }

    @Override
    public long getFirstTime(String mac) {
        return dao.loadFirstTime(mac) * 1000L;
    }

    @Override
    public boolean isDegree(String mac){
        return BaseDatabase.getInstance().deviceDao().isDegree(mac);
    }

    @Override
    public Flowable<DeviceInfo> getInfo(String mac) {
        return BaseDatabase.getInstance().deviceDao().loadDeviceInfo(mac);
    }
}
